<?php
namespace app\massage\model;

use app\BaseModel;
use think\facade\Db;

class ChannelList extends BaseModel
{
    //定义表名
    protected $name = 'massage_channel_list';


    protected $append = [

        'cate_text'

    ];

    public function getMobileAttr($value,$data){

        if(!empty($value)&&isset($data['uniacid'])){

            if(numberEncryption($data['uniacid'])==1){

                return  substr_replace($value, "****", 2,4);
            }

        }

        return $value;

    }
    /**
     * @param $value
     * @param $data
     * @功能说明:返回渠道
     * @author chenniang
     * @DataTime: 2022-09-02 16:32
     */
    public function getCateTextAttr($value,$data){

        if(isset($data['cate_id'])){

            $cate_model = new ChannelCate();

            return $cate_model->where(['id'=>$data['cate_id']])->value('title');

        }


    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10,$field='*'){

        $data = $this->where($dis)->field($field)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }



    /**
     * @author chenniang
     * @DataTime: 2021-12-30 11:26
     * @功能说明:后台列表
     */
    public function adminDataList($dis,$page=10,$where=[]){

        $data = $this->alias('a')
            ->join('massage_service_user_list b','a.user_id = b.id','left')
            ->where($dis)
            ->where(function ($query) use ($where){
                $query->whereOr($where);
            })
            ->field('a.*,b.nickName,b.avatarUrl')
            ->group('a.id')
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2023-04-23 11:58
     * @功能说明:渠道商提成比例 业务员
     */
    public function channelBalance($order){

        $config = getConfigSettingArr($order['uniacid'],['channel_balance','salesman_balance','channel_coach_balance','channel_admin_balance','salesman_coach_balance','salesman_admin_balance']);

        $order = array_merge($order,$config);
        //渠道商平台承担比例
        $order['channel_company_balance'] = 100-$order['channel_coach_balance'] - $order['channel_admin_balance'];
        //业务员平台承担比例
        $order['salesman_company_balance']= 100-$order['salesman_coach_balance'] - $order['salesman_admin_balance'];

        if(!empty($order['channel_id'])){

            $channel = $this->dataInfo(['id'=>$order['channel_id'],'status'=>2]);

            if(!empty($channel)){

                $channel['channel_id'] = $channel['id'];

                $salesman_model = new Salesman();

                $salesman = $salesman_model->dataInfo(['id'=>$channel['salesman_id'],'status'=>2]);

                if(!empty($salesman)){

                    $order['salesman_id'] = $salesman['id'];

                }
            }else{

                $channel['channel_id'] = 0;
            }

        }

        return $order;
    }



    /**
     * @param $data
     * @功能说明:是否是管理员
     * @author chenniang
     * @DataTime: 2023-05-06 16:50
     */
    public function checkAuthData($data){

        $cap_dis[] = ['user_id','=',$data['id']];

        $cap_dis[] = ['status','in',[1,2,3,4]];
        //查看是否是团长
        $cap_info = $this->where($cap_dis)->order('id desc')->find();

        $cap_info = !empty($cap_info)?$cap_info->toArray():[];
        //-1表示未申请团长，1申请中，2已通过，3取消,4拒绝
        $arr['channel_status'] = !empty($cap_info)?$cap_info['status']:-1;

        $arr['channel_text'] = !empty($cap_info)?$cap_info['sh_text']:'';

        return $arr;

    }
   









}